<template>
  <el-container class="home_container">
    <el-header>
      <div>
        <img src="../assets/img/heima.png" alt="" />
        <h1>电商后台管理系统</h1>
      </div>
      <el-button type="info" @click="logOut">退出</el-button>
    </el-header>
    <el-container>
      <el-aside :width="isCollapse ? '64px' : '200px'">
        <div class="toggle_btn" @click="toggleCollapse">|||</div>
        <el-menu
          class="el-menu-vertical-demo"
          background-color="#313743"
          text-color="#fff"
          active-text-color="#409EFF"
          unique-opened
          :collapse="isCollapse"
          :collapse-transition="false"
          router
          :default-active="activePath"
        >
          <!-- 一级菜单 -->
          <el-submenu
            :index="(index + 1).toString()"
            v-for="(item, index) in menuList"
            :key="item.id"
          >
            <!-- 一级菜单模板区域 -->
            <template slot="title">
              <!-- 图标 -->
              <i :class="iconsObj[item.id]"></i>
              <!-- 文本 -->
              <span>{{ item.authName }}</span>
            </template>
            <!-- 二级菜单 -->
            <el-menu-item
              :index="'/' + item1.path"
              v-for="(item1, index1) in item.children"
              :key="item1.id"
              @click="saveActivePath('/' + item1.path)"
            >
              <template slot="title">
                <i class="el-icon-menu"></i>
                <span>{{ item1.authName }}</span>
              </template>
            </el-menu-item>
          </el-submenu>
        </el-menu>
      </el-aside>
      <el-main>
        <router-view></router-view>
      </el-main>
    </el-container>
  </el-container>
</template>

<script>
export default {
  name: 'Home',
  data() {
    return {
      menuList: [],
      iconsObj: {
        '125': 'iconfont icon-users',
        '103': 'iconfont icon-tijikongjian',
        '101': 'iconfont icon-shangpin',
        '102': 'iconfont icon-danju',
        '145': 'iconfont icon-baobiao'
      },
      //是否折叠菜单栏
      isCollapse: false,
      //用于保存当前菜单的激活状态
      activePath: ''
    }
  },
  created() {
    //获取菜单列表数据
    this.getMenuList()
    //获取被保存的菜单激活状态
    this.activePath = sessionStorage.getItem('activePath')
  },
  methods: {
    //登出
    logOut() {
      //删除token缓存
      window.sessionStorage.removeItem('token')
      //登出实现跳转
      this.$router.push('/login')
    },
    //获取侧边菜单列表数据
    async getMenuList() {
      const { data: res } = await this.$http.get('menus')
      if (res.meta.status !== 200) return this.$message.error(res.meta.msg)
      this.menuList = res.data
      // console.log(res)
    },
    //点击按钮折叠侧边栏
    toggleCollapse() {
      this.isCollapse = !this.isCollapse
    },
    //点击切换菜单激活状态
    saveActivePath(activePath) {
      //将当前菜单的激活状态保存在sessionStorage中
      window.sessionStorage.setItem('activePath', activePath)
      //点击菜单时切换
      this.activePath = activePath
    }
  }
}
</script>

<style lang="less" scoped>
.home_container {
  height: 100%;
  .el-header {
    background-color: #363d40;
    display: flex;
    justify-content: space-between;
    align-items: center;
    div {
      display: flex;
      align-items: center;
      h1 {
        color: #fff;
        margin-left: 20px;
      }
    }
  }
  .el-aside {
    background-color: #313743;
    .toggle_btn {
      font-size: 12px;
      background-color: #475163;
      text-align: center;
      color: #fff;
      line-height: 24px;
      letter-spacing: 0.2em;
      cursor: pointer;
    }
    .el-menu {
      border-right: none;
    }
  }
  .el-main {
    background-color: #e9edf1;
  }
}
.iconfont {
  margin-right: 10px;
}
</style>
